//
//  VisitorLoginView.m
//  MYChatting
//
//  Created by mac on 16/7/25.
//  Copyright © 2016年 lhf. All rights reserved.
//

#import "VisitorLoginView.h"

@interface VisitorLoginView ()

@property (nonatomic,weak) UIImageView *cicleView;
@property (nonatomic,weak) UIImageView *largeIcon;
@property (nonatomic,weak) UILabel *mytextLabel;
@property (nonatomic,weak) UIButton *loginBtn;
@property (nonatomic,weak) UIButton *registerBtn;
@property (nonatomic,weak) UIImageView *backView;

@end





@implementation VisitorLoginView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}



- (void)setUpUI{
     UIImageView *cicleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    self.cicleView = cicleView;
    
    
     UIImageView *largeIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    self.largeIcon = largeIcon;
    
    UILabel *mytextLabel = [[UILabel alloc] init];
    mytextLabel.text = @"登陆后，别人评论你的微博，发给你的消息，都会在这里收到通知";
    mytextLabel.numberOfLines = 0;
    mytextLabel.textColor = [UIColor darkGrayColor];
    mytextLabel.textAlignment = NSTextAlignmentCenter;
    mytextLabel.font = [UIFont systemFontOfSize:14];
    [mytextLabel sizeToFit];
    self.mytextLabel = mytextLabel;
    
    UIButton *loginBtn = [[UIButton alloc] init];
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
    [loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [loginBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [loginBtn sizeToFit];
    self.loginBtn = loginBtn;
    
    UIButton *registerBtn = [[UIButton alloc] init];
    [registerBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:themeColor forState:UIControlStateNormal];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [registerBtn sizeToFit];
    self.registerBtn = registerBtn;
    
    UIImageView *backView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    self.backView = backView;
    
    [self addSubview:self.cicleView];
    [self addSubview:self.backView];
    [self addSubview:self.largeIcon];
    [self addSubview: self.mytextLabel];
    [self addSubview:self.loginBtn];
    [self addSubview:self.registerBtn];
  
    
    //布局位置约束
#warning 在系统的手码添加约束时，需要屏蔽系统的frame布局
    for (UIView *subView in self.subviews) {
        subView.translatesAutoresizingMaskIntoConstraints = false;
    }
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.largeIcon attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.largeIcon attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:-60]];
    
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.cicleView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.largeIcon attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.cicleView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.largeIcon attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.mytextLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.cicleView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.mytextLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.cicleView attribute:NSLayoutAttributeBottom multiplier:1 constant:16]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.mytextLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:230]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.mytextLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:40]];
    
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.mytextLabel attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mytextLabel attribute:NSLayoutAttributeBottom multiplier:1 constant:20]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:36]];
    
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.mytextLabel attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mytextLabel attribute:NSLayoutAttributeBottom multiplier:1 constant:20]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:36]];
    
#pragma mark 可视化布局
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[backView]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"backView":self.backView}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[backView]-(-35)-[regBtn]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"backView":self.backView,@"regBtn":self.registerBtn}]];
    self.backgroundColor = [UIColor colorWithWhite:0.93 alpha:1];
    
    
    [self.loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.registerBtn addTarget:self action:@selector(registerBtnClick) forControlEvents:UIControlEventTouchUpInside];
}



#pragma mark VisitorLoginViewDelegate代理方法
- (void)loginBtnClick{
   
    if ([self.delegate respondsToSelector:@selector(userWillLogin)]) {
        [self.delegate userWillLogin];
    }
}

- (void)registerBtnClick{
  
    if ([self.delegate respondsToSelector:@selector(userWillRegister)]) {
        [self.delegate userWillRegister];
    }
}



- (void)setInfo:(NSString *)infoStr andImageStr:(NSString *)imageStr{
    self.mytextLabel.text = infoStr;
    
    
    if (imageStr) {
        self.largeIcon.image = [UIImage imageNamed:imageStr];
        self.cicleView.hidden = YES;
        [self bringSubviewToFront:self.largeIcon];
       
    }else{
        [self startAnimation];
    }
}


- (void)startAnimation{
  CABasicAnimation *basicAnaimation = [[CABasicAnimation alloc] init];
  basicAnaimation.keyPath = @"transform.rotation";
    basicAnaimation.repeatCount = MAXFLOAT;
    basicAnaimation.duration = 20.0;
    basicAnaimation.toValue = @(2 * M_PI);
    basicAnaimation.removedOnCompletion = false;
    [self.cicleView.layer addAnimation:basicAnaimation forKey:nil];
    
}


@end
